#include "udp_includes.h"


size_info si_kti_Report[]
={
	{1,1},{1,1},{1,1},{2,1}
	,{1,sizeof(KTI_SubData_Alarm_T)} ,{1,10}, {2,1}, {1,15},{1,1}
	,{4,1},{4,1} ,{1,1} ,{4,1},{1,8}
	,{1,1},{1,1} ,{1,1},{1,1} ,{1,1},{1,1}, {1,1},{1,1},{1,8}
	,{0,0} // notify end of data
};
size_info si_kti_StatusReq[]
={
	{0,0} // notify end of data
};

size_info si_kti_Alarm[]
={
	{1,sizeof(KTI_SubData_Alarm_T)}
	,{0,0} // notify end of data
};
size_info si_kti_CtrlReq[]
={
	{1,1} ,{1,10} ,{2,1} ,{1,10}
	,{0,0} // notify end of data
};

size_info si_kti_IpChangeReq[]
={
	{1,4} ,{2,1} ,{2,1}
	,{0,0} // notify end of data
};
size_info si_kti_IpChangeRsp[]
={
	{0,0} // notify end of data
};


size_info si_kti_ResetReq[]
={
	{0,0} // notify end of data
};
size_info si_kti_ResetRsp[]
={
	{0,0} // notify end of data
};


size_info si_kti_MacReq[]
={
	{0,0} // notify end of data
};
size_info si_kti_MacRsp[]
={
	{1,6} ,{1,1} ,{2,1}
	,{0,0} // notify end of data
};


size_info si_kti_DLCmdReq[]
={
	{1,2},{1,1},{1,1},{2,1},{2,1}
	,{0,0} // notify end of data
};
size_info si_kti_DLCmdRsp[]
={
	{1,2},{1,1},{1,1},{2,1},{1,1},{1,1}
	,{0,0} // notify end of data
};


size_info si_kti_DLData[]
={
	{2,1},{2,1}
	,{0,0} // notify end of data
};
size_info si_kti_DLDataRsp[]
={
	{2,1},{2,1}
	,{0,0} // notify end of data
};



size_info si_kti_LocalReq[]
={
	{0,0} // notify end of data
};
size_info si_kti_LocalRsp[]
={
	{0,0} // notify end of data
};


size_info si_kti_DLCompleteReq[]
={
	{1,2},{1,1},{1,1},{2,1},{2,1}
	,{0,0} // notify end of data
};
size_info si_kti_DLCompleteRsp[]
={
   	      {1,1},{1,1},{2,1},{1,1},{2,1}
	,{0,0} // notify end of data
};

size_info si_kti_HearBeatReq[]
={
   	{1,1},{1,1}
	,{0,0} // notify end of data
};

size_info si_kti_HearBeatRsp[]
={
	{1,1}
	,{0,0} // notify end of data
};

size_info_by_command sibc[]={
		{KTC_STATUS_REQ	,si_kti_StatusReq,"KTC_STATUS_REQ"}
		,{ KTC_STATUS_REPORT	,si_kti_Report," KTC_STATUS_REPORT"}

		,{KTC_ALARM_REPORT	,si_kti_Alarm,"KTC_ALARM_REPORT"}
		,{KTC_CONTRL_REQ	,si_kti_CtrlReq,"KTC_CONTRL_REQ"}

		,{KTC_IP_CHANGE_REQ	,si_kti_IpChangeReq,"KTC_IP_CHANGE_REQ"}
		,{KTC_IP_CHANGE_RSP	,si_kti_IpChangeRsp,"KTC_IP_CHANGE_RSP"}

		,{KTC_DL_CMD_REQ		,si_kti_DLCmdReq,		"KTC_DL_CMD_REQ"}
		,{KTC_DL_CMD_RSP		,si_kti_DLCmdRsp,		"KTC_DL_CMD_RSP"}

		,{KTC_DL_DATA			,si_kti_DLData,			"KTC_DL_DATA"}
		,{KTC_DL_DATA_RSP		,si_kti_DLDataRsp,		"KTC_DL_DATA_RSP"}

		,{KTC_DL_COMPLETE_REQ	,si_kti_DLCompleteReq,	"KTC_DL_COMPLETE_REQ"}
		,{KTC_DL_COMPLETE_RSP	,si_kti_DLCompleteRsp,	"KTC_DL_COMPLETE_RSP"}

		,{KTC_RESET_REQ		,si_kti_ResetReq,"KTC_RESET_REQ"}
		,{KTC_RESET_RSP		,si_kti_ResetRsp,"KTC_RESET_RSP"}

		,{KTC_MAC_ADDR_REQ	,si_kti_MacReq,"KTC_MAC_ADDR_REQ"}
		,{KTC_MAC_ADDR_RSP	,si_kti_MacRsp,"KTC_MAC_ADDR_RSP"}

		,{KTC_LOCAL_REQ	,si_kti_LocalReq,"KTC_LOCAL_REQ"}
		,{KTC_LOCAL_RSP	,si_kti_LocalRsp,"KTC_LOCAL_RSP"}

		,{KTC_HEARTBEAT_REQ	,si_kti_HearBeatReq,"KTC_HEARTBEAT_REQ"}
		,{KTC_HEARTBEAT_RSP	,si_kti_HearBeatRsp,"KTC_HEARTBEAT_RSP"}

		//,{KTC_IP_CHANGE_RSP	,si_kti_IpChangeRsp}
		//,{KTC_RESET_REQ		,si_kti_ResetReq}
		//,{KTC_RESET_RSP		,si_kti_ResetRsp}
		//,{KTC_MAC_ADDR_REQ	,si_kti_MacReq}
		//,{KTC_STATUS_REQ	,si_kti_StatusReq}
		,{0,NULL} // notify end of data
};

char *GetCmdTitle(int8 cmd)
{
	size_info_by_command *p_sibc=sibc;
	char *none_dummy=" ";
	int i=0;
	//search struct size info to meet cmd
	while( p_sibc[i].p!=NULL)
	{
		if( p_sibc[i].cmd == cmd )break;
		i++;
	}

	if( p_sibc[i].p == NULL ) return none_dummy; // fail to find cmd
	else return p_sibc[i].title;

}

void my_htons(char *p)
{
	char c[2];
	c[0]=p[0]; c[1]=p[1];
	p[0]=c[1]; p[1]=c[0];
}
void my_htonl(char *p)
{
	char c[4];
	c[0]=p[0]; c[1]=p[1]; c[2]=p[2]; c[3]=p[3];
	p[0]=c[3]; p[1]=c[2]; p[2]=c[1]; p[3]=c[0];
}
void my_ntohs(char *p)
{
	char c[2];
	c[0]=p[0]; c[1]=p[1];
	p[0]=c[1]; p[1]=c[0];
}
void my_ntohl(char *p)
{
	char c[4];
	c[0]=p[0]; c[1]=p[1]; c[2]=p[2]; c[3]=p[3];
	p[0]=c[3]; p[1]=c[2]; p[2]=c[1]; p[3]=c[0];
}

int msg_ntoh_data(int8 *p_buf,int8u cmd)
{
	size_info_by_command *p_sibc=sibc;
	size_info *p_s;
	int8 *p ;
	int16 *p16 ;
	int32 *p32 ;
	int j;
	int i=0;

	//search struct size info to meet cmd
	while( p_sibc[i].p!=NULL)
	{
		if( p_sibc[i].cmd == cmd )break;
		i++;
	}

	if( p_sibc[i].p == NULL ) return 0; // fail to find cmd

	p_s = p_sibc[i].p; i=0; //assign struct size info pointer to meet cmd
	p = p_buf;
	while( p_s[i].type!=0)
	{
		if( p_s[i].type == 2)
		{
			//udp_printf("\n 0x%02X%02X",p[0]&0xFF,p[1]&0xFF);
			my_ntohs( p );
			//udp_printf("--> 0x%02X%02X",p[0]&0xFF,p[1]&0xFF);
		}
		else
		if( p_s[i].type == 4)
		{
			//udp_printf("\n 0x%02X%02X%02X%02X",p[0]&0xFF,p[0+1]&0xFF,p[0+2]&0xFF,p[0+3]&0xFF);
			my_ntohl( p );
			//udp_printf("-->0x%02X%02X%02X%02X\n",p[0]&0xFF,p[0+1]&0xFF,p[0+2]&0xFF,p[0+3]&0xFF);
		}
		p += ( p_s[i].type*p_s[i].size ) ;
		i++;
	}
	//start to swap
	return 0;
}

int msg_hton_data(int8 *p_buf,int8u cmd)
{
	size_info_by_command *p_sibc=sibc;
	size_info *p_s;
	int8  *p ;
//	int16 PACKED  *p16;
//	int32 PACKED *p32;
	char c[4];
	int j;
	int i=0;
	int offset=0;
	if( cmd == KTC_DL_CMD_REQ ){
    	j=0;
	}
	//search struct size info to meet cmd
	while( p_sibc[i].p!=NULL)
	{
		if( p_sibc[i].cmd == cmd ){
			break;
		}
		i++;
	}
	udp_printf("msg_hton_data:0x%X",p_sibc[i].cmd);///
	if( p_sibc[i].p == NULL ) return 0; // fail to find cmd

	p_s = p_sibc[i].p; i=0; //assign struct size info pointer to meet cmd
	p = p_buf;
	udp_printf("<%d,p:%p>\n",offset,p);
	while( p_s[i].type!=0)
	{
		if( p_s[i].type == 2)
		{
			//udp_printf("\n 0x%02X%02X",p[0]&0xFF,p[1]&0xFF);
			my_htons(p);
			//udp_printf("-->0x%02X%02X\n",p[0]&0xFF,p[1]&0xFF);
		}
		else
		if( p_s[i].type == 4)
		{
			//udp_printf("\n 0x%02X%02X%02X%02X",p[0]&0xFF,p[0+1]&0xFF,p[0+2]&0xFF,p[0+3]&0xFF);
			my_htonl(p);
			//udp_printf("-->0x%02X%02X%02X%02X\n",p[0]&0xFF,p[0+1]&0xFF,p[0+2]&0xFF,p[0+3]&0xFF);
		}
		else
		{
			//udp_printf("<%d,p:%p ,p+offset:%p >8:0x%02X\n",offset,p ,(int8*)(p+offset),p[offset]);
		}
		//offset += ( p_s[i].type*p_s[i].size ) ;
		p+=( p_s[i].type*p_s[i].size ) ;
		i++;
	}
	//start to swap
	return 0;
}
